package com.kfm.service;

import com.kfm.dao.StudentDao;
import com.kfm.entity.Student;
import com.kfm.entity.StudentDTO;
import com.kfm.mapper.IStudentMapper;
import com.kfm.util.Page;

import java.sql.SQLException;
import java.util.List;

public class StudentService implements IStudentService{

    private IStudentMapper dao;

    public StudentService(IStudentMapper mapper){
        dao = mapper;
    }

    @Override
    public List<Student> list(){
        return dao.selectAll();
    }

    @Override
    public Student findById(String id){
        return dao.selectById(id);
    }

    /**
     *
     * @param page 页码
     * @param size 页大小
     * @return
     */
    @Override
    public Page<StudentDTO> listWithTeacherNameAndClassName(String page, String size){
        int pageNo = 1;
        int start = 0;
        int pageSize = 1;
        if (size != null){
            pageSize = Integer.parseInt(size);
        }
        if (page != null){
            pageNo = Integer.parseInt(page);
            start = (pageNo - 1) * pageSize;
        }
        List<StudentDTO> studentDTOS = dao.selectAllWithTeacherNameAndClassName(start, pageSize);
        int total = dao.count();
        return new Page<StudentDTO>(pageNo, pageSize, total, studentDTOS);
    }
    @Override
    public int save(Student stu){
        return dao.save(stu);
    }

    @Override
    public int update(Student stu){
        return dao.updateById(stu);
    }

    @Override
    public int delete(String id){
        return dao.delete(id);
    }
}
